﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tunnel.DevicesControls
{
    public partial class COVI : DeviceBase
    {
        string deviceID;
        string shortName;
        string name;
        string typeID;
        string devicePos;
        int picX;
        int picY;
        string stationCode;
        string groupID;
        string commType;
        string protocol;
        string port;
        int factoryID;
        int controllable;
        DateTime updateTime;
        string updateBy;
        string standardID;
        string tunnel;

        string typeName;
        string subSys;

        string statusID;
        DateTime rsTime;
        string statusDescription;

        string paramAddr;
        string paramValue;
        DateTime cmdTime;
        string runned;

        decimal comax;
        decimal comin;
        decimal vimax;
        decimal vimin;

        decimal co;
        decimal vi;

        private event EventHandler DeviceIdChanged;
        private event EventHandler COVIChanged;

        public string DeviceID
        {
            get
            {
                return deviceID;
            }

            set
            {
                deviceID = value;
                DeviceIdChanged(this, new EventArgs());
            }
        }

        public string ShortName
        {
            get
            {
                return shortName;
            }

            set
            {
                shortName = value;
            }
        }

        public string Name1
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        public string TypeID
        {
            get
            {
                return typeID;
            }

            set
            {
                typeID = value;
            }
        }

        public string DevicePos
        {
            get
            {
                return devicePos;
            }

            set
            {
                devicePos = value;
            }
        }

        public int PicX
        {
            get
            {
                return picX;
            }

            set
            {
                picX = value;
            }
        }

        public int PicY
        {
            get
            {
                return picY;
            }

            set
            {
                picY = value;
            }
        }

        public string StationCode
        {
            get
            {
                return stationCode;
            }

            set
            {
                stationCode = value;
            }
        }

        public string GroupID
        {
            get
            {
                return groupID;
            }

            set
            {
                groupID = value;
            }
        }

        public string CommType
        {
            get
            {
                return commType;
            }

            set
            {
                commType = value;
            }
        }

        public string Protocol
        {
            get
            {
                return protocol;
            }

            set
            {
                protocol = value;
            }
        }

        public string Port
        {
            get
            {
                return port;
            }

            set
            {
                port = value;
            }
        }

        public int FactoryID
        {
            get
            {
                return factoryID;
            }

            set
            {
                factoryID = value;
            }
        }

        public int Controllable
        {
            get
            {
                return controllable;
            }

            set
            {
                controllable = value;
            }
        }

        public DateTime UpdateTime
        {
            get
            {
                return updateTime;
            }

            set
            {
                updateTime = value;
            }
        }

        public string UpdateBy
        {
            get
            {
                return updateBy;
            }

            set
            {
                updateBy = value;
            }
        }

        public string StandardID
        {
            get
            {
                return standardID;
            }

            set
            {
                standardID = value;
            }
        }

        public string Tunnel
        {
            get
            {
                return tunnel;
            }

            set
            {
                tunnel = value;
            }
        }

        public string TypeName
        {
            get
            {
                return typeName;
            }

            set
            {
                typeName = value;
            }
        }

        public string SubSys
        {
            get
            {
                return subSys;
            }

            set
            {
                subSys = value;
            }
        }

        public string StatusID
        {
            get
            {
                return statusID;
            }

            set
            {
                statusID = value;
            }
        }

        public DateTime RsTime
        {
            get
            {
                return rsTime;
            }

            set
            {
                rsTime = value;
            }
        }

        public string StatusDescription
        {
            get
            {
                return statusDescription;
            }

            set
            {
                if (value == "正常")
                {
                    this.Image = Respic.一氧化碳检测器正常;
                }
                if (value == "故障")
                {
                    this.Image = Respic.一氧化碳检测器故障;
                }
                

                statusDescription = value;
            }
        }

        public string ParamAddr
        {
            get
            {
                return paramAddr;
            }

            set
            {
                paramAddr = value;
            }
        }

        public string ParamValue
        {
            get
            {
                return paramValue;
            }

            set
            {
                paramValue = value;
            }
        }

        public DateTime CmdTime
        {
            get
            {
                return cmdTime;
            }

            set
            {
                cmdTime = value;
            }
        }

        public string Runned
        {
            get
            {
                return runned;
            }

            set
            {
                runned = value;
            }
        }

        public decimal Comax
        {
            get
            {
                return comax;
            }

            set
            {
                comax = value;
            }
        }

        public decimal Comin
        {
            get
            {
                return comin;
            }

            set
            {
                comin = value;
            }
        }

        public decimal Vimax
        {
            get
            {
                return vimax;
            }

            set
            {
                vimax = value;
            }
        }

        public decimal Vimin
        {
            get
            {
                return vimin;
            }

            set
            {
                vimin = value;
            }
        }

        public decimal Co
        {
            get
            {
                return co;
            }

            set
            {
                co = value;
                COVIChanged(this, new EventArgs());
            }
        }

        public decimal Vi
        {
            get
            {
                return vi;
            }

            set
            {
                vi = value;
                COVIChanged(this, new EventArgs());
            }
        }

        public COVI()
        {
            InitializeComponent();
            this.StatusDescription = "正常";
            this.DeviceIdChanged += OnDeviceIdChanged;
            this.COVIChanged += OnCOVIChanged;
        }
        /// <summary>
        /// 判断COVI值是否在限定值范围内
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnCOVIChanged(object sender, EventArgs e)
        {
            coviPanelShow.covalue.Text = Co.ToString();
            coviPanelShow.vivalue.Text = Vi.ToString();
            //CO值超上界
            if (Co>Comax)
            {
                if (!(coviPanelShow.covalue.ForeColor == Color.Red))
                {
                    coviPanelShow.covalue.ForeColor = Color.Red;
                    //此处添加报警代码

                }
            }
            //CO值超下界
            if (Co<Comin)
            {
                if (!(coviPanelShow.covalue.ForeColor == Color.Blue))
                {
                    coviPanelShow.covalue.ForeColor = Color.Blue;
                    //此处添加报警代码

                }
            }
            //CO值恢复
            if(Co <= Comax&& Co >= Comin)
            {
                coviPanelShow.covalue.ForeColor = Color.White;
            }
            //VI值超上界
            if (Vi>Vimax)
            {
                if (!(coviPanelShow.vivalue.ForeColor == Color.Red))
                {
                    coviPanelShow.vivalue.ForeColor = Color.Red;
                    //此处添加报警代码

                }
            }
            //VI值超上界
            if (Vi<Vimin)
            {
                if (!(coviPanelShow.vivalue.ForeColor == Color.Blue))
                {
                    coviPanelShow.vivalue.ForeColor = Color.Blue;
                    //此处添加报警代码

                }
            }
            //VI值恢复
            if (Vi <= Vimax&& Vi >= Vimin)
            {
                coviPanelShow.vivalue.ForeColor = Color.White;
            }

        }

        private void OnDeviceIdChanged(object sender, EventArgs e)
        {
            bwSearchMaxMin.RunWorkerAsync();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            //this.infomationPanel.Parent = this.Parent;
            //this.infomationPanel.Location = new Point(this.Location.X-84, this.Location.Y +30);
            //this.infomationPanel.BringToFront();
            //this.infomationPanel.Show();
            this.coviPanelShow.Parent = this.Parent;
            this.coviPanelShow.Location = new Point(this.Location.X - 84, this.Location.Y + 30);
            this.coviPanelShow.BringToFront();
            if(this.coviPanelShow.IsDisposed)
            {
                coviPanelShow = new COVIPanel();
            }
            this.coviPanelShow.Show();
        }

        public static explicit operator COVI(string v)
        {
            throw new NotImplementedException();
        }

        private void bwSearchMaxMin_DoWork(object sender, DoWorkEventArgs e)
        {
            string sqlSearchMaxMin = "select * from M_ParamLimit where DeviceTypeID='COVI'";
            SqlDBHelper SDH = new Tunnel.SqlDBHelper();
            DataTable dtMaxMin = SDH.GetTable(sqlSearchMaxMin);
            e.Result = dtMaxMin;
        }

        private void bwSearchMaxMin_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            DataTable dtMaxMin = e.Result as DataTable;
            foreach(DataRow dr in dtMaxMin.Rows)
            {
                if(dr["ParamType"].ToString()=="CO")
                {
                    Comax = Convert.ToDecimal(dr["HighAlrLimit"].ToString());
                    Comin = Convert.ToDecimal(dr["LowAlrLimit"].ToString());
                    coviPanelShow.comax.Text = dr["HighAlrLimit"].ToString();
                    coviPanelShow.comin.Text = dr["LowAlrLimit"].ToString();
                }
                if(dr["ParamType"].ToString() == "VI")
                {
                    Vimax = Convert.ToDecimal(dr["HighAlrLimit"].ToString());
                    Vimin = Convert.ToDecimal(dr["LowAlrLimit"].ToString());
                    coviPanelShow.vimax.Text = dr["HighAlrLimit"].ToString();
                    coviPanelShow.vimin.Text = dr["LowAlrLimit"].ToString();
                }
            }
        }
    }
}
